<?php
/**
 * Created by PhpStorm.
 * User: 15814
 * Date: 2018/10/9
 * Time: 10:38
 */
include "config.php";

if ($_POST["p_size"]!=null){
    if ($_SERVER["REQUEST_METHOD"]=="POST"){
        if (isset($_POST["uId"])  && isset($_POST["p_id"])){
            $select="SELECT * FROM `goodsinfo` WHERE `gId`=? AND `uId`=?;";
            $res=$conn->prepare($select);
            $res->bind_param("ii",$_POST["p_id"],$_POST["uId"]);
            $res->execute();
            $row=$res->get_result();
            $toal=intval($_POST["gNum"])*intval($_POST["p_price"]);
            //查询有结果修改数据
            if ($row->num_rows>=1){
                $update="UPDATE `goodsinfo` SET `gNum`=`gNum`+?,`gtoal`=`gNum`*`gPrice`;";
                $ures=$conn->prepare($update);
                $ures->bind_param("i",$_POST["gNum"]);
                $ures->execute();
                if($ures->affected_rows>=1){
                    print_r(json_encode(array("status"=>2,"msg"=>"修改成功")));
                }
                else{
                    print_r(json_encode(array("status"=>-2,"msg"=>"修改失败")));
                }
            }

            //未查询到增加数据
            else{
                $insert="INSERT INTO `goodsinfo` (`gId`,`gName`,`gPrice`,`gSize`,`gColor`,`gNum`,`gtoal`,`uId`,`gimg`)
                VALUE (?,?,?,?,?,?,?,?,?);";
                $ires=$conn->prepare($insert);
                $ires->bind_param("isissiiis",$_POST["p_id"],$_POST["p_name"],$_POST["p_price"],$_POST["p_size"],$_POST["p_color"],$_POST["gNum"],$toal,$_POST["uId"],$_POST["p_img"]);
                $ires->execute();
                if ($ires->affected_rows>=1){
                    print_r(json_encode(array("status"=>1,"msg"=>"加入购物车成功")));

                }
                else{
                    print_r(json_encode(array("status"=>-1,"msg"=>"加入购物车失败")));

                }
            }

        }
        //参数不合法时提示
        else{
            print_r(json_encode(array("status"=>-3,"msg"=>"参数不合法")));
        }
    }



}
